"""

专用测试签名/模板
签名名称 【 阿里云短信测试
使用场景 【发送测试短信
模版名称  【测试专用模板
模版Code  【SMS_154950909
模版类型  【验证码
模版内容  【您正在使用阿里云短信测试服务，体验验证码是：${code}，如非本人操作，请忽略本短信！

"""
import json
from web_shop import settings
from alibabacloud_dysmsapi20170525.client import Client
from alibabacloud_tea_openapi.models import Config
from alibabacloud_dysmsapi20170525.models import SendSmsRequest
from alibabacloud_tea_util.models import RuntimeOptions


class AliyunSMS:
    # #必填，您的 AccessKey ID,
    # access_key_id = settings.ACCESS_Key_ID
    # # 必填，您的 AccessKey Secret,
    # access_key_secret = settings.ACCESS_Key_SECRET
    endpoint = 'dysmsapi.aliyuncs.com'
    sign_name = '阿里云短信测试'
    template_code = 'SMS_154950909'

    def __init__(self):
        # 1，创建一个配置对象
        self.config = Config(
            # 必填，您的 AccessKey ID,
            access_key_id=settings.ACCESS_Key_ID,
            # 必填，您的 AccessKey Secret,
            access_key_secret=settings.ACCESS_Key_SECRET,
            endpoint=self.endpoint
        )

    def send_msg(self, mobile: str, code: str):
        """
        发送短信
        :param mobile: 手机号
        :param code: 验证码
        :return:
        """
        # 1，创建一个客户端
        client = Client(self.config)
        # 2，创建短信对象
        send_sms_request = SendSmsRequest(
            sign_name=self.sign_name,
            # template_param='{"code":"205714"}',
            template_param=json.dumps({"code": code}),
            template_code=self.template_code,
            phone_numbers=mobile,
        )
        # 3，设置允许时间选项
        runtime = RuntimeOptions()
        # 4，发送短信
        try:
            res = client.send_sms_with_options(send_sms_request, runtime)
            if res.body.code == 'OK':
                return {'code': 'OK', "message": '短信发送成功'}
            else:
                return {'code': 'NO', "error": res.body.message}
        except Exception as e:
            return {'code': 'NO', "error": '短信发送失败'}


if __name__ == '__main__':
    AliyunSMS().send_msg(mobile='17713995975', code='369258')

# class Sample:
#     def create_client(self, access_key_id: str, access_key_secret: str, ):
#         # 1，创建一个配置对象
#         config = Config(
#             # 必填，您的 AccessKey ID,
#             access_key_id=access_key_id,
#             # 必填，您的 AccessKey Secret,
#             access_key_secret=access_key_secret,
#             endpoint=f'dysmsapi.aliyuncs.com'
#         )
#         # 2，创建一个客户端
#         client = Client(config)
#
#         # 3，创建短信对象
#         send_sms_request = SendSmsRequest(
#             sign_name='阿里云短信测试',
#             template_code='SMS_154950909',
#             phone_numbers='17713995975',
#             template_param='{"code":"205714"}'
#         )
#         # 4，设置允许时间选项
#         runtime = RuntimeOptions()
#         # 5，发送短信
#         client.send_sms_with_options(send_sms_request, runtime)
